/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pa165.dominatingspecies.frontend.animal;

import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.prepost.PreAuthorize;
import pa165.dominating.species.service.EnvironmentService;
import pa165.dominating.species.tos.AnimalTo;
import pa165.dominating.species.tos.EnvironmentTo;
import pa165.dominatingspecies.frontend.pages.BasePage;
import pa165.dominatingspecies.service.impl.AnimalServiceImpl;

/**
 *
 * @author Filip
 */
@PreAuthorize("hasRole('WORKER')")
public class EditAnimalPage extends BasePage {

    @SpringBean(name = "animalService")
    private AnimalServiceImpl animalService;
    @SpringBean(name = "environmentService")
    private EnvironmentService environmentService;
    private AnimalTo animal;
    private EnvironmentTo selectedEnvironment;
    private EnvironmentTo filter = null;

    EditAnimalPage(Long id, EnvironmentTo filter) {
        this.filter = filter;
        setPage(id);
    }

    EditAnimalPage(Long id) {
        setPage(id);
    }

    private void setPage(Long id) {
        if (id != null) {
            animal = animalService.getAnimalById(id);
        }
        List<EnvironmentTo> environments = environmentService.getAllEnvironments();
        selectedEnvironment = animal.getEnvironmentTo();

        Form<AnimalTo> form = new Form<>("createAnimalForm");
        form.setModel(new CompoundPropertyModel<>(animal));
        form.add(new Label("nameLabel", new StringResourceModel("animalNameLabel", this, null)));
        form.add(new TextField<String>("name"));
        form.add(new Label("speciesEdit", new StringResourceModel("animalSpeciesLabel", this, null)));
        form.add(new TextArea<String>("species"));
        form.add(new Label("environment", new StringResourceModel("selectEnvironment", this, null)));
        Link<BasePage> backToList = new Link<BasePage>("backToList") {
            @Override
            public void onClick() {
                AnimalsPage ep = new AnimalsPage(filter);
                setResponsePage(ep);
            }
        };
        form.add(backToList);

        form.add(new DropDownChoice<>("environmentsEdit", new PropertyModel<EnvironmentTo>(this, "selectedEnvironment"), environments));

        Button submitButton = new Button("submitButton") {
            @Override
            public void onSubmit() {
                boolean errorsFound = false;
                if (animal.getName() == null) {
                    getSession().warn(new StringResourceModel("animalNameEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (animal.getSpecies() == null) {
                    getSession().warn(new StringResourceModel("animalSpeciesEmptyWarn", this, null).getString());
                    errorsFound = true;
                }
                if (selectedEnvironment == null) {
                    getSession().warn(new StringResourceModel("animalNoEnvironmentSelectedWarn", this, null).getString());
                    errorsFound = true;
                }

                if (!errorsFound) {
                    boolean thrown = false;
                    try {
                        animal.setEnvironmentTo(selectedEnvironment);
                        animalService.updateAnimal(animal);
                    } catch (DataAccessException ex) {
                        getSession().warn(new StringResourceModel("animalUpdateFail", this, null, new Object[]{animal.getId()}).getString());
                        System.out.println(ex.getMessage());
                        thrown = true;
                    }
                    if (!thrown) {
                        getSession().info(new StringResourceModel("animalUpdateSucc", this, null, new Object[]{animal.getId()}).getString());
                        setResponsePage(new AnimalsPage(filter));
                    }
                }
            }
        };
        form.add(submitButton);
        add(form);
    }

    public EnvironmentTo getSelectedEnvironment() {
        return selectedEnvironment;
    }

    public void setSelectedEnvironment(EnvironmentTo selectedEnvironment) {
        this.selectedEnvironment = selectedEnvironment;
    }
}
